﻿using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TD.Interface.Credits.Effects
{
    internal class Efx_Special : Effect
    {
        private enum Stage { Intro, Slide, Outro, Overtime }

        private Matrix matrix;
        private Stage stage;
        private float angle;
        private float triggerCount;

        public Efx_Special() :
            base(10) // Trigger is fired this amount of miliseconds.
        {
            Reset();
        }

        public override void Reset()
        {
            matrix = Matrix.Identity;
            stage = Stage.Intro;
            angle = 90.0f;
            triggerCount = 0;
        }

        protected override void TriggerEfx()
        {
            if (stage == Stage.Intro)
            {
                angle -= 0.1f;
                if (angle <= 0.0f)
                {
                    angle = 0.0f;
                    stage = Stage.Slide;
                }
            }
            if (stage != Stage.Overtime) // Slide for an X amount of triggers.
            {
                triggerCount += 0.2f;
                if (stage == Stage.Slide && triggerCount >= 100.0f)
                {
                    //triggerCount = 150.0f;
                    stage = Stage.Outro;
                    angle = 0.0f;
                }
            }
            if (stage == Stage.Outro)
            {
                angle += 0.2f;
                if (angle >= 90.0f && stage != Stage.Overtime)
                {
                    angle = 90.0f;
                    stage = Stage.Overtime;
                    Overtime();
                }
            }

            float radians = ((float)(Math.PI * angle / 180.0f));
            matrix = Matrix.CreateRotationY(radians);
            matrix *= Matrix.CreateTranslation(-triggerCount, 0.0f, 0.0f);

            base.TriggerEfx();
        }

        protected override Matrix[] ApplyMatrices()
        {
            return new Matrix[1] { matrix };
        }
    }
}
